﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhao.Common.Helper;
using Zhao.Models.DTO;
using Zhao.Models.Entity;


namespace Zhao.Common.MapConfig
{
    public class AutoMapperConfigs : Profile
    {
        /// <summary>
        /// 配置映射关系，在实例化这个类的时候，就要处理好
        /// </summary>
        public AutoMapperConfigs()
        {
            #region 用户实体与DTO映射
            CreateMap<Users, UsersDto>()
                .ForMember(dest => dest.id, opt => opt.MapFrom(src => src.UserID))
                .ForMember(dest => dest.user, opt => opt.MapFrom(src => src.Username))
                .ForMember(dest => dest.name, opt => opt.MapFrom(src => src.RealName))
                .ForMember(dest => dest.phone, opt => opt.MapFrom(src => src.Phone))
                .ForMember(dest => dest.money, opt => opt.MapFrom(src => src.AccountBalance))
                .ForMember(dest => dest.status, opt => opt.MapFrom(src => src.Status))
                .ForMember(dest => dest.Sex, opt => opt.MapFrom(src => src.Sex))
                .ForMember(dest => dest.Avatar, opt => opt.MapFrom(src => src.Avatar))
                .ReverseMap()  // 配置反向映射
                .ForMember(dest => dest.UserID, opt => opt.MapFrom(src => src.id))
                .ForMember(dest => dest.Username, opt => opt.MapFrom(src => src.user))
                .ForMember(dest => dest.RealName, opt => opt.MapFrom(src => src.name))
                .ForMember(dest => dest.Phone, opt => opt.MapFrom(src => src.phone))
                .ForMember(dest => dest.AccountBalance, opt => opt.MapFrom(src => src.money))
                .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.status));

            // 从数据库实体 FieldTypeEntity 映射到 DTO FieldTypeDto
            CreateMap<FieldTypes, FieldTypesDto>()
                .ForMember(dest => dest.id, opt => opt.MapFrom(src => src.FieldTypeID))
                .ForMember(dest => dest.TypeName, opt => opt.MapFrom(src => src.FieldTypeName))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));

            // 反向映射：从 DTO FieldTypeDto 映射到数据库实体 FieldTypeEntity
            CreateMap<FieldTypesDto, FieldTypes>()
                .ForMember(dest => dest.FieldTypeID, opt => opt.MapFrom(src => src.id))
                .ForMember(dest => dest.FieldTypeName, opt => opt.MapFrom(src => src.TypeName))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));


            CreateMap<Roles, RolesDto>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.RoleID))
                .ForMember(dest => dest.roleName, opt => opt.MapFrom(src => src.RoleName))
                .ForMember(dest => dest.description, opt => opt.MapFrom(src => src.Description))
                .ForMember(dest => dest.status, opt => opt.MapFrom(src => src.Status))
                .ForMember(dest => dest.createTime, opt => opt.MapFrom(src => src.CreateTime));

            CreateMap<RolesDto, Roles>()
                .ForMember(dest => dest.RoleID, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.RoleName, opt => opt.MapFrom(src => src.roleName))
                .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.description))
                .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.status))
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.createTime))
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            CreateMap<Menus, MenuDTO>()
                           .ForMember(s => s.ParentId, m => m.MapFrom(x => x.ParentId == null ? default(Guid) : x.ParentId))
                           .ReverseMap();

            CreateMap<Menus, TreeSelectDTO>()
              .ForMember(a => a.Label, m => m.MapFrom(x => x.MenuText))
              .ForMember(a => a.Value, m => m.MapFrom(x => x.Id.ToString()))
              .ForMember(a => a.Children, m => m.MapFrom(x => x.Children));

            CreateMap<Roles, Rolesintro>()
              .ForMember(a => a.Id, m => m.MapFrom(x => x.RoleID));

            CreateMap<Venues, VenusInfo>()
                .ForMember(a => a.Id, m => m.MapFrom(x => x.VenueID))
                .ForMember(a => a.Name, m => m.MapFrom(x => x.VenueName));


            CreateMap<Venues, VenueDto>()
           .ForMember(dest => dest.VenueId, opt => opt.MapFrom(src => src.VenueID))
           .ForMember(dest => dest.Image, opt => opt.MapFrom(src => src.Images))
           .ForMember(dest => dest.provinceId, opt => opt.MapFrom(src => src.Province))
           .ForMember(dest => dest.cityId, opt => opt.MapFrom(src => src.City))
           .ForMember(dest => dest.districtId, opt => opt.MapFrom(src => src.Zone))
           .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.IsDeleted))
           .ForMember(dest => dest.rRules, opt => opt.MapFrom(src => src.ReservationRules))
           .ForMember(dest => dest.OpenTime, opt => opt.MapFrom(src => src.OpenTime.ToString(@"hh\:mm")))
           .ForMember(dest => dest.CloseTime, opt => opt.MapFrom(src => src.CloseTime.ToString(@"hh\:mm")))
           .ForMember(dest => dest.LicenseImage, opt => opt.MapFrom(src => src.License));

            // Reverse mapping if needed
            CreateMap<VenueDto, Venues>()
                .ForMember(dest => dest.VenueID, opt => opt.MapFrom(src => src.VenueId))
                .ForMember(dest => dest.Images, opt => opt.MapFrom(src => src.Image))
                .ForMember(dest => dest.City, opt => opt.MapFrom(src => src.cityId))
                .ForMember(dest => dest.Province, opt => opt.MapFrom(src => src.provinceId))
                .ForMember(dest => dest.Zone, opt => opt.MapFrom(src => src.districtId))
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(src => src.Status))
                .ForMember(dest => dest.ReservationRules, opt => opt.MapFrom(src => src.rRules))
                .ForMember(dest => dest.OpenTime, opt => opt.MapFrom(src => TimeSpan.Parse(src.OpenTime)))
                .ForMember(dest => dest.CloseTime, opt => opt.MapFrom(src => TimeSpan.Parse(src.CloseTime)));

            CreateMap<VenuesImage, VenusImageDto>()
                .ForMember(a => a.Id, m => m.MapFrom(x => x.Id))
                .ForMember(a => a.Imager, m => m.MapFrom(x => x.Imager));

            CreateMap<VenusImageDto, VenuesImage>()
                .ForMember(a => a.Id, m => m.MapFrom(x => x.Id))
                .ForMember(a => a.Imager, m => m.MapFrom(x => x.Imager));

            CreateMap<Notifications, NotificationDTO>()
                .ReverseMap();

            #endregion

            #region 分页模型映射
            CreateMap<PageModel<Users>, PageModel<UsersDto>>()
                .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data))
                .ReverseMap()
                .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data));

            CreateMap<PageModel<FieldTypes>, PageModel<FieldTypesDto>>()
               .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data))
               .ReverseMap()
               .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data));

            CreateMap<PageModel<Roles>, PageModel<RolesDto>>()
               .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data))
               .ReverseMap()
               .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data));

            CreateMap<PageModel<Menus>, PageModel<MenuDTO>>()
               .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data))
               .ReverseMap()
               .ForMember(dest => dest.data, opt => opt.MapFrom(src => src.data));


            #endregion

        }
    }
}
